package cn.edu.nankai.bookstore.controller.book;

import cn.edu.nankai.bookstore.domain.po.Book;
import cn.edu.nankai.bookstore.service.BookService;
import cn.edu.nankai.bookstore.service.impl.BookServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "AddBookServlet", value = "/AddBookServlet")
public class AddBookServlet extends HttpServlet {
    BookService bookService = new BookServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();
        if(session.getAttribute("su") == null){
            response.getWriter().write("{access:'deny'}");
            return;
        }
        String name = (String) request.getParameter("name");
        Double price = Double.parseDouble((String) request.getParameter("price"));
        Integer id = bookService.add(new Book(name,price));
        response.getWriter().write(id.toString());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
